#include <stdlib.h>
static const digit_table[] = {
	1,	
	10,
	100,
	1000,
	10000,
	100000,
	1000000,
	10000000,
	100000000,
	1000000000
};
static inline int get_max_num(int *data, int data_lenth)
{
	int i = 0;
	int max = 0;
	for (i = 0; i < data_lenth; i++)
	{
		if (data[i] > max)
		{
			max = data[i];
		}
	}
	return max;
}
static inline int get_digit(int data)
{
	int digit = 0;
	while (data /= 10)
	{
		digit++;
	}
	digit++;
	return digit;
}

int radix_sort(int *data, int data_lenth)
{
	if (data == 0 || data_lenth <= 0)
	{
		return -1;
	}
	int i = 0, j = 0, z = 0;
	int max_digit = 0;


	int *data_tmp = (int *)malloc(data_lenth * sizeof(int));
	int *data_falgs = (int *)malloc(data_lenth * sizeof(int));

	int *p_src = data_tmp;
	int *p_dst = data;

	max_digit = get_digit(get_max_num(data, data_lenth));

	for (i = 0; i < max_digit; i++)
	{
		if (p_src == data)
		{
			p_src = data_tmp;
			p_dst = data;
		}
		else
		{
			p_src = data;
			p_dst = data_tmp;
		}

		/** 标记对应位数 各个数据的基数 **/
		for (j = 0; j < data_lenth; j++)
		{
			data_falgs[j] = (p_src[j] / digit_table[i]) % 10 ;
		}
		/** 基数由 0 ~ 9 依次存放在另外一个数组,等于实现一次排序 **/
		for ( z = 0 ; z < 10 ; z++)
		{
			for (j = 0; j < data_lenth; j++)
			{
				if (data_falgs[j] == z)
				{
					*p_dst++ = p_src[j];	
				}
			}	
		}
	}
	if (p_src == data)
	{
		memcpy(data, data_tmp, data_lenth * sizeof(int));
	}
	return 0;
}

	
	

